package com.eChartsOperation.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.eChartsOperation.service.StockChartsService;
@RequestMapping("stockCharts")
@Controller
public class StockChartsController {
	
	@Autowired
	private StockChartsService stockChartsServiceImp;
	
	
	
	/**
	 * 查询缺货率信息
	 * @param request
	 * @param response
	 * @return
	 *  dateInfo-日期
	 */
	@RequestMapping("selStockoutInfo")
	@ResponseBody
	public Map<String, List<String>> selStockoutInfo(HttpServletRequest request,HttpServletResponse response){
		 
		Map<String, List<String>> stockoutInfo=new HashMap<String, List<String>>();
		 String startDate=request.getParameter("startDate");
		 String endDate=request.getParameter("endDate");
		 
		 //查询日期(一个时间段的所有日期)
		 List<String> dateInfo=stockChartsServiceImp.selStockDate(startDate, endDate);	 
		 stockoutInfo.put("dateInfo",dateInfo);			 
		 //查询计算缺货率的仓库信息
		 List<String> warehousInfo=stockChartsServiceImp.selWarehouse();
		 warehousInfo.add("00=总仓");
		 //仓库名
		 List<String> warehousName= new ArrayList<String>();	 		 
		 //查询条件
		 Map<String, String> conditionMap= new HashMap<String, String>();
		 conditionMap.put("startDate", startDate);
		 conditionMap.put("endDate", endDate);
		 conditionMap.put("categoryOne", request.getParameter("categoryOne"));
		 conditionMap.put("categoryTwo", request.getParameter("categoryTwo"));
		 conditionMap.put("categoryThree", request.getParameter("categoryThree"));
		 conditionMap.put("productBrand", request.getParameter("productBrand"));
		 for (String warehouse_id : warehousInfo) {
			 //查询单个仓库的缺货率
			conditionMap.put("warehouseId", warehouse_id.split("=")[0]);
			List<String> stockoutRate=stockChartsServiceImp.selStockoutRate(conditionMap);			
			stockoutInfo.put(warehouse_id.split("=")[1], stockoutRate);			
			warehousName.add(warehouse_id.split("=")[1]);
		 }  
		 stockoutInfo.put("warehousName", warehousName);
		 
		 return stockoutInfo;
	}
	
	
	/**
	 * 查询单个仓库商品总个数
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("selStouckoutTotalNumber")
	@ResponseBody
	public Integer selStouckoutTotalNumber(HttpServletRequest request,HttpServletResponse response){
		 //查询条件
		 Map<String, Object> conditionMap= new HashMap<String, Object>();	 
		 conditionMap.put("currentDate", request.getParameter("currentDate"));
		 conditionMap.put("warehouseName",(request.getParameter("warehouseName").equals("总仓")==true)?null:request.getParameter("warehouseName"));
		 conditionMap.put("categoryOne", request.getParameter("categoryOne"));
		 conditionMap.put("categoryTwo", request.getParameter("categoryTwo"));
		 conditionMap.put("categoryThree", request.getParameter("categoryThree"));
		 conditionMap.put("productBrand", request.getParameter("productBrand"));		 		
		return stockChartsServiceImp.selStouckoutTotalNumber(conditionMap);
	}
	
	/**
	 * 查询缺货明细
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("selStockoutDetailed")
	@ResponseBody
	public Map<String, Object> selStockoutDetailed(HttpServletRequest request,HttpServletResponse response){
		 //查询条件
		 Map<String, Object> conditionMap= new HashMap<String, Object>();
		 conditionMap.put("warehouseName",(request.getParameter("warehouseName").equals("总仓")==true)?null:request.getParameter("warehouseName"));
		 conditionMap.put("currentDate", request.getParameter("currentDate"));
		 conditionMap.put("categoryOne", request.getParameter("categoryOne"));
		 conditionMap.put("categoryTwo", request.getParameter("categoryTwo"));
		 conditionMap.put("categoryThree", request.getParameter("categoryThree"));
		 conditionMap.put("productBrand", request.getParameter("productBrand"));
		 //easyui分页
		 Integer page =Integer.parseInt(request.getParameter("page"));
		 Integer rows =Integer.parseInt(request.getParameter("rows"));
		 conditionMap.put("pageNum",(page-1)*rows);
		 conditionMap.put("pageSize",rows);	
		 //数据
		 List<Map<String, Object>> stockoutDetailed=stockChartsServiceImp.selStockoutDetailed(conditionMap);
		 //总条数
		 Integer stockoutCount=stockChartsServiceImp.selStockoutCount(conditionMap);
		 
		 //easyui数据表格
		 Map<String, Object> jsonMap= new HashMap<String, Object>();
		 jsonMap.put("rows", stockoutDetailed);
		 jsonMap.put("total",stockoutCount);
		 
		return jsonMap;
	}
	
	
	/**
	 * 查询商品类别
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("selProductCategory")
	@ResponseBody
	public List<Map<String,String>> selProductCategory(HttpServletRequest request,HttpServletResponse response){
		//查询条件
		Map<String, String> map =new HashMap<String, String>();
		//判断类别一
		if (request.getParameter("categoryOne")!=null){
			 map.put("type","two");			 
			 map.put("categoryOne",request.getParameter("categoryOne"));
			//判断类别二
			if (request.getParameter("categoryTwo")!=null) {
				map.put("type","three");
				map.put("categoryOne",request.getParameter("categoryOne"));
				map.put("categoryTwo",request.getParameter("categoryTwo"));			
			}												
		}else{						
			map.put("type","one");
		}
		List<Map<String, String>> ss=stockChartsServiceImp.selProductCategory(map);
		
		return stockChartsServiceImp.selProductCategory(map);
	}
	
	/**
	 * 查询品牌
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("selProductBrand")
	@ResponseBody
	public List<Map<String, String>> selProductBrand(HttpServletRequest request,HttpServletResponse response){	 
		return stockChartsServiceImp.selProductBrand();
	}
		

}
